<template>
	<view>
		<!-- 搜索框 -->
	<!-- 	<u-search placeholder="搜索徒步路线或活动" shape="round"></u-search> -->

		<!-- 帖子列表 -->
		<view class="post-list">

			<view class="flex flex-column margin-top background padding" v-for="(item, index) in postsInfo"
				:key="index">

				<view class="flex flex-row user-info">
					<u-avatar size="50px" :src="item.user.avatarUrl"></u-avatar>
					<view class="margin-left">
						<u-text color="#000000" :text="item.user.username"></u-text>
						<u-text color="grey" :text="formatDate(item.createdAt)"></u-text>
					</view>
				</view>

				<!-- 标题 内容 -->
				<view @click="goToDetails(item.postId)" class="post-content">
					<u-text class="padding-top" lineHeight="27px" color="black" size="18" :text="item.content"></u-text>
				</view>
				<!-- 图片 -->
			<!-- 	<view class="image-row flex"  >
					{{image.imageUrl}}
					<u-image v-for="(image, indexs) in item.images" :key="indexs" class="image-item" height="90px"
						width="100%" :src="image.imageUrl" />
				</view> -->
	

				<view class="flex flex-row justify-around margin-top post-actions" @click="addLike(item.postId)">
					<u-icon labelPro="left" :label="item.likeCount" name="thumb-up" size="25px"></u-icon>
					<u-icon class="margin-left" :label="item.likeCount" name="chat" size="25px"></u-icon>
				</view>

				<view class="publish-btn" @click="addPosts">
					<!-- <u-icon name="plus" size="24" color="black"></u-icon> -->
					<u-button type="primary" :plain="true" shape="circle" icon="plus"></u-button>
				</view>

			</view>

			<!-- 固定在右下角的发布帖子图标按钮 -->

		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				urls2: [
				    'https://cdn.uviewui.com/uview/album/1.jpg',
				    'https://cdn.uviewui.com/uview/album/2.jpg',
				    'https://cdn.uviewui.com/uview/album/3.jpg',
				    'https://cdn.uviewui.com/uview/album/4.jpg',
				    'https://cdn.uviewui.com/uview/album/5.jpg',
				    'https://cdn.uviewui.com/uview/album/6.jpg',
				    'https://cdn.uviewui.com/uview/album/7.jpg'
				],
				postsInfo: []
			};
		},
		onLoad() {
			this.getData()
		},
		methods: {
			getData() {
				uni.request({
					url: 'http://localhost:8083/posts',
					method: "GET",
					success: (res) => {
						this.postsInfo = res.data.data
					},
					fail: (err) => {
						console.log("请求错误")
					}
				})
			},
			goToDetails(id) {
				console.log(id)
				uni.navigateTo({
					url: "/pages/community/postsDetails/postsDetails?postId=" + id
				})
			},
			//点赞或者添加
			addLike(postId) {
				console.log("实现点赞或者更新")
			},
			addPosts() {
				
				uni.navigateTo({
					url: "/pages/community/publish/publish"
				})
				console.log("实现点赞或者更新")
			},
			//时间格式化
			formatDate(dateString) {
				const date = new Date(dateString);
				const year = date.getFullYear();
				const month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的，所以加1
				const day = ('0' + date.getDate()).slice(-2);
				return `${year}-${month}-${day}`;
			},


		}

	};
</script>

<style lang="scss">
	.post-list {
		display: flex;
		flex-direction: column;
	}

	.user-info {
		display: flex;
		align-items: center;
	}

	.post-content {
		cursor: pointer;
	}

	.image-row {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
	}

	.image-item {
		flex: 0 0 calc(33.333% - 10px);
		margin: 5px;
		height: 90px;
		object-fit: cover;
	}

	.post-actions {
		cursor: pointer;
	}

	.publish-btn {
		position: fixed;
		bottom: 60px;
		right: 25px;
		z-index: 999;
		/* 确保按钮在最上层 */
		cursor: pointer;
	}
</style>